import axios from 'axios'
import { Message } from 'element-ui'

const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API || 'http://localhost:9001',
  timeout: 5000,
  withCredentials: true
})

service.interceptors.request.use(
  config => {
    console.log('发送请求到:', config.url, '数据:', config.data)
    const token = localStorage.getItem('token')
    if (token) {
      config.headers['Authorization'] = 'Bearer ' + token
    }
    return config
  },
  error => {
    console.error('请求配置错误:', error)
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  response => {
    console.log('收到响应:', response.data)
    return response.data
  },
  error => {
    console.error('请求失败:', error.config)
    console.error('错误详情:', error.message)
    
    Message({
      message: '服务器连接失败，请确认服务器是否启动',
      type: 'error',
      duration: 5000
    })
    return Promise.reject(error)
  }
)

export default service 